/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package controladores;

import entidades.LoteIngresoMaterial;
import entidades.Material;
import entidades.SubTipoMaterial;
import entidades.TipoMaterial;
import entidades.UnidadMedida;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;

/**
 *
 * @author g
 */
public class MaterialJpaControllerExt {
    
    public static final int CODIGO = 1;
    public static final int COD_BARRAS = 2;
    public static final int DESCRIPCION = 3;
    
    public MaterialJpaControllerExt(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }
    
    
    //Metodos Extra para SubtipoMaterial
    public List<SubTipoMaterial> getSubTipoMaterialPorTipoMaterial(Long id) {
        EntityManager em = getEntityManager();
        List<SubTipoMaterial> result = null;
        try {
            String jpql = "SELECT m FROM SubTipoMaterial m JOIN m.tipoMaterial mat WHERE mat.id =:id";
            Query query = em.createQuery(jpql);
            query.setParameter("id", id);
            result = query.getResultList();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return (result);
    }
    
    public boolean yaExisteSubTipoMaterial(String value, Long id_excepcion) {
        EntityManager em = getEntityManager();
        List<SubTipoMaterial> result = null;
        try {
            String jpql = "SELECT p FROM SubTipoMaterial p WHERE p.descripcion =:value";            
            Query query = em.createQuery(jpql);
            query.setParameter("value", value);
            result = query.getResultList();
            if(result.isEmpty())
                return false;
            else{
               for(SubTipoMaterial v: result){
                   if(v.getId().equals(id_excepcion))
                       return false;
               }
               return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return true;
    }
    
    public boolean yaExisteTipoMaterial(String value, Long id_excepcion) {
        EntityManager em = getEntityManager();
        List<TipoMaterial> result = null;
        try {
            String jpql = "SELECT p FROM TipoMaterial p WHERE p.descripcion =:value";            
            Query query = em.createQuery(jpql);
            query.setParameter("value", value);
            result = query.getResultList();
            if(result.isEmpty())
                return false;
            else{
               for(TipoMaterial v: result){
                   if(v.getId().equals(id_excepcion))
                       return false;
               }
               return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return true;
    }
    
    public boolean yaExisteUnidadMedida(String value, Long id_excepcion) {
        EntityManager em = getEntityManager();
        List<UnidadMedida> result = null;
        try {
            String jpql = "SELECT p FROM UnidadMedida p WHERE p.descripcion =:value";            
            Query query = em.createQuery(jpql);
            query.setParameter("value", value);
            result = query.getResultList();
            if(result.isEmpty())
                return false;
            else{
               for(UnidadMedida v: result){
                   if(v.getId().equals(id_excepcion))
                       return false;
               }
               return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return true;
    }
    
    //Metodos Extras para Material
    public List<Material> getMaterialesPor(String value, String parametro) {
        EntityManager em = getEntityManager();
        List<Material> result = null;
        try {
            String jpql = "";
            switch (parametro) {                               
                case "TipoMaterial":
                    jpql = "SELECT ma FROM Material ma JOIN ma.subTipoMaterial subtipo JOIN subtipo.tipoMaterial tipomat WHERE tipomat.descripcion =:valor";
                    break;      
                case "SubTipoMaterial":
                    jpql = "SELECT ma FROM Material ma JOIN ma.subTipoMaterial subtipo WHERE subtipo.descripcion =:valor";
                    break;
                case "UnidadMedida":
                    jpql = "SELECT ma FROM Material ma JOIN ma.unidadMedida unidad WHERE unidad.descripcion =:valor";
                    break;        
            }
            Query query = em.createQuery(jpql);
            query.setParameter("valor", value);
            result = query.getResultList();
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return (result);
    }
    
    public boolean yaExiste(String value, int parametro, Long id_excepcion) {
        EntityManager em = getEntityManager();
        List<Material> result = null;
        try {
            String jpql = "";
            switch (parametro) {                
                case MaterialJpaControllerExt.CODIGO:
                    jpql = "SELECT a FROM Material a WHERE a.codigo =:valor";
                    break;                
                case MaterialJpaControllerExt.DESCRIPCION:
                    jpql = "SELECT a FROM Material a WHERE a.descripcion =:valor";
                    break;
                case MaterialJpaControllerExt.COD_BARRAS:
                    jpql = "SELECT a FROM Material a WHERE a.codigo_barras =:valor";
                    break;    
            }
            Query query = em.createQuery(jpql);
            query.setParameter("valor", value);
            result = query.getResultList();
            if (result.isEmpty()) {
                return false;
            } else {
                for (Material v : result) {
                    if (v.getId().equals(id_excepcion)) {
                        return false;
                    }
                }
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return true;
    }
    
    public List<Material> getMaterialesLike(String value, int parametro) {
        EntityManager em = getEntityManager();
        List<Material> result = null;
        try {
            String jpql = "";
            switch (parametro) {              
                case MaterialJpaControllerExt.CODIGO:
                    jpql = "SELECT a FROM Material a WHERE a.codigo LIKE :valor";
                    break;                
                case MaterialJpaControllerExt.DESCRIPCION:
                    jpql = "SELECT a FROM Material a WHERE a.descripcion LIKE :valor";
                    break;
                case MaterialJpaControllerExt.COD_BARRAS:
                    jpql = "SELECT a FROM Material a WHERE a.codigo_barras LIKE :valor";
                    break;    
            }
            Query query = em.createQuery(jpql);
            query.setParameter("valor", "%" + value + "%");
            result = query.getResultList();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return (result);
    }
        
    public Long getUltimoLoteDeIngresoDeMateriales() {
        EntityManager em = getEntityManager();
        String jpql = "SELECT ing FROM LoteIngresoMaterial ing ORDER BY ing.id DESC";
        Query q = em.createQuery(jpql);
        List<LoteIngresoMaterial> resultList = q.getResultList();
        if (!resultList.isEmpty()) {
            return resultList.get(0).getId();
        }
        return null;

    }
    
}
